const http = require('http');
const path = require('path');
const url = require('url');
const ejs = require('ejs');
const app = require('./model/express-router');

http.createServer(app).listen(3000);

let ROOT_PATH = path.resolve(__dirname);
console.log(ROOT_PATH)

app.get('/', function(req, res){
    let msg = '这是从数据库读取的内容';
    ejs.renderFile(ROOT_PATH + '/views/index.ejs',{msg:msg}, function(err, data){
        if(err){
            console.log(err);
        }
        else{
            res.send(data);
        }
    });
});

//登录页面
app.get('/login', function(req, res){
    console.log('login');
    ejs.renderFile(ROOT_PATH + '/views/form.ejs', {}, function(err, data){
        if(err){
            console.log(err);
        }
        else{
            res.send(data);
        }
    })
});

//执行登录
app.post('/doLogin', function(req, res){
    console.log(req.body);
    res.send("<script>alert('登录成功');history.back();</script>");
});

//注册
app.get('/register', function(req, res){
    console.log('register');
    res.send('register');
})

//news
app.get('/news', function(req, res){
    console.log('register');
    res.send('新闻数据');
})